package com.flymobi.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import com.flymobi.fw.modules.CommonService;
import com.flymobi.fw.util.StringUtil;
import com.flymobi.po.SmsLog;
import com.flymobi.po.SmsTask;
import com.flymobi.util.C;
import com.flymobi.vo.SmsLogSearchVo;

public class SmsLogService extends CommonService {
	public List<SmsLog> list(SmsLogSearchVo param,int startRow, int pagePerSize) {
		String hql="from SmsLog m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(!StringUtil.isEmpty(param.getMobile())&&param.getMobile().trim().length()>0){
				hql+=" and m.mobile=?";
				paramList.add(param.getMobile());
			}
			if(param.getTaskId()!=null&&param.getTaskId()!=-1){
				hql+=" and m.taskId=?";
				paramList.add(param.getTaskId());
			}
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		hql+=" order by m.id desc";
		//System.out.println("hql:"+hql);
		//System.out.println("param:"+param.toString());
		return dao.find(SmsLog.class,hql, paramList,startRow, pagePerSize);
	}
	
	public Long getCount(SmsLogSearchVo param) {
		String hql="select count(*) from SmsLog m where 1=1 ";
		List<Object> paramList=new ArrayList<Object>();
		if(param!=null){
			if(!StringUtil.isEmpty(param.getMobile())&&param.getMobile().trim().length()>0){
				hql+=" and m.mobile=?";
				paramList.add(param.getMobile());
			}
			if(param.getTaskId()!=null&&param.getTaskId()!=-1){
				hql+=" and m.taskId=?";
				paramList.add(param.getTaskId());
			}
			if(param.getStatus()!=null&&param.getStatus()!=-1){
				hql+=" and m.status =?";
				paramList.add(param.getStatus());
			}
			if(param.getEnd()!=null){
				hql+=" and m.savetime<?";
				paramList.add(param.getEnd());
			}
			if(param.getStart()!=null){
				hql+=" and m.savetime>?";
				paramList.add(param.getStart());
			}
		}
		return dao.getCount(hql, paramList);
	}
	
	public SmsLog findByMySeq(String seqNum){
		return dao.findSingle(SmsLog.class,"from SmsLog m where m.mySeqNum=?",new Object[]{seqNum});
	}
	
	public SmsLog findBySpSeq(String seqNum){
		return dao.findSingle(SmsLog.class,"from SmsLog m where m.spSeqNum=?",new Object[]{seqNum});
	}
	
	public long getCount(long taskId){
		return dao.getCount("select count(*) from SmsLog m where m.taskId=?",new Object[]{taskId});
	}
	
	public long getSuccessCount(long taskId){
		return dao.getCount("select count(*) from SmsLog m where m.taskId=? and m.errorCode=0",new Object[]{taskId});
	}
	
	public long getCount(long taskId,long status){
		return dao.getCount("select count(*) from SmsLog m where m.taskId=? and m.status=?",new Object[]{taskId,status});
	}

	public void save(SmsLog users) {
		dao.save(users);
	}

	public SmsLog load(String id) {
		return dao.load(SmsLog.class, new Long(id));
	}

	public SmsLog get(String id) {
		return dao.get(SmsLog.class, new Long(id));
	}

	public void delete(String id) {
		dao.delete(this.get(id));
	}
	
	public void save(SmsTask task,File file) {
		try {
			BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));  
			String line;
			int j=0;
			while ((line = br.readLine()) != null) {
				if(!StringUtil.isEmpty(line)&&StringUtil.isMobile(line.trim())){
					j++;
					System.out.println("mobile :"+j+":"+line);
					SmsLog smsLog=new SmsLog();
					smsLog.setMobile(line.trim());
					smsLog.setMsg(task.getMessage());
					smsLog.setStatus(0);
					smsLog.setTaskId(task.getId());
					smsLog.setErrorCode(-1);
					smsLog.setSpNumber(C.sgip.SPNumber);
					dao.save(smsLog);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
